import os,shutil,io
import re
import sys
import face_recognition
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
def get_name_path(name):
    file_dir = r'D:\image\用户'
    find_name=re.split("-",name)[-1][0:-4]
    #print(find_name)
    for filr in os.listdir(file_dir):
        #print(filr)
        if (filr.find(find_name)!=-1):
            return os.path.join(file_dir,filr)
    return -1
def bijiao(url_1,url_2):
    #  导入待检测照片
    face_image1 = face_recognition.load_image_file(url_1)
    face_image2 = face_recognition.load_image_file(url_2)
    #  进行人脸识别
    face_encoding1 = face_recognition.face_encodings(face_image1)
    face_encoding2 = face_recognition.face_encodings(face_image2)
    #  获得面部位置
    face_location1 = face_recognition.face_locations(face_image1)
    face_location2 = face_recognition.face_locations(face_image2)
    #  得到识别数据
    try:
        face1 = face_encoding1[0]
        face2 = face_encoding2[0]
        #  人脸数据比对
        result = face_recognition.compare_faces([face1], face2, tolerance=0.5)
        #  如果结果是同一人显示PASS，否则显示NO
    except:
        print("not face")
        return -1
    if result == [True]:
        print("pass")
    else:
        print("NO")
def star(path_1):
    path_2 = get_name_path(path_1)
    print(path_2)
    if (path_2 != -1):
        bijiao(path_1, path_2)
if __name__ == '__main__':
    star(sys.argv[1])
